package com.jsmtr.rms.controller.DUserResume;

import com.jsmtr.rms.entity.BaseUser;
import com.jsmtr.rms.entity.DUserResume;
import com.jsmtr.rms.entity.DoSSUser;
import com.jsmtr.rms.entity.ProjectInfoFlow;
import com.jsmtr.rms.service.IDoMSService;
import com.jsmtr.rms.service.IDoResumeService;
import com.jsmtr.rms.service.IProjectService;
import com.jsmtr.rms.service.IUtilService;
import com.jsmtr.rms.utils.EnumTools;
import com.jsmtr.rms.utils.StringTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 简历状态重置
 * @author: cyx
 * @description:
 * @date: Created in 2020-3-17 15:47
 * @modified by:
 */
@Controller
public class DUserResumeResetController {
    @Resource
    private IDoResumeService doResumeService;
    @Resource
    private IUtilService utilService;
    @Resource
    private IProjectService projectService;

    @RequestMapping(value = "/DUserResume/ResetAction")
    public void RAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String ids = request.getParameter("ids");
            if (ids == null) {
                utilService.printJsonResult(false, "至少选择一条数据！", "no", response);
            } else {
                String fid = request.getParameter("fid").trim();//招聘流程ID
                String deptcode = request.getParameter("deptcode").trim();//责任部门编码
                String[] uids=ids.split(",");
                BaseUser cuser = utilService.getSessionUser(request);
                if (cuser.getUdept().equals(deptcode)) {
                    if (utilService.FlowIsDoing(fid)) {
                        List<DUserResume> editList = new ArrayList<DUserResume>();
                        List<DUserResume> users = doResumeService.getResumes(uids);
                        ProjectInfoFlow flow=projectService.getFolwById(fid);
                        for (DUserResume user : users) {
                            if(user.getProid().indexOf("D_")>=0)
                                user=fillFowResultSZ(user,request,flow,cuser);
                            else
                                user=fillFowResult(user,request,flow,cuser);
                            editList.add(user);
                        }

                        if (editList.size() > 0) {
                            utilService.editList(editList);
                            utilService.printJsonResult(true, "操作成功！", "no", response);
                        } else {
                            utilService.printJsonResult(false, "当前选中信息不支持此操作！", "no", response);
                        }
                    } else {
                        utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败", "no", response);
        }
    }

    public DUserResume fillFowResult(DUserResume resume,HttpServletRequest request,ProjectInfoFlow nowflow,BaseUser operator){
        String flowName=nowflow.getFname();
        if (!StringTools.trimNotEmpty(flowName)){
            flowName=nowflow.getNickname();
        }
        if (flowName.equalsIgnoreCase(EnumTools.FlowNomalState.简历筛选.name())){
            resume.setState("未处理");
            // resume.setSxbz("");
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.笔试.name())){
            //
            // resume=fillExamResult(resume,newstate,newremark,nowflow,operator);
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.资料审核.name())){
            resume.setZlstate("未处理");
            resume.setZldate(null);
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.面试.name())){

        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.背景调查.name())||flowName.equalsIgnoreCase("政审")){
            resume.setZsstate("未处理");
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.实作.name())){
            // resume.
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.体检.name())){
            resume.setTjstate("未处理");
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.签约.name())){
            resume.setQystate("未处理");
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.实习.name())){
            resume.setSxstate("未处理");
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.录用.name())){
            resume.setBdstate("未处理");
        }
        return  resume;
    }

    public DUserResume fillFowResultSZ(DUserResume resume,HttpServletRequest request,ProjectInfoFlow nowflow,BaseUser operator){
        String flowName=nowflow.getFname();
        if (!StringTools.trimNotEmpty(flowName)){
            flowName=nowflow.getNickname();
        }
        if (flowName.equalsIgnoreCase(EnumTools.NZFlowState.报名筛选.name())){
            resume.setState("未处理");
            // resume.setSxbz("");
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.笔试.name())){

        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.面试.name())){

        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.录用.name())){
            resume.setBdstate("未处理");
        }
        return  resume;
    }
    @RequestMapping(value = "/DUserResume/QAResetAction")
    public void QAResetAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String ids = request.getParameter("ids");
            if (ids == null) {
                utilService.printJsonResult(false, "至少选择一条数据！", "no", response);
            } else {
                String fid = request.getParameter("fid").trim();//招聘流程ID
                String deptcode = request.getParameter("deptcode").trim();//责任部门编码
                String[] uids=ids.split(",");
                BaseUser cuser = utilService.getSessionUser(request);
                if (cuser.getUdept().equals(deptcode)) {
                    if (utilService.FlowIsDoing(fid)) {
                        List<DoSSUser> editList = new ArrayList<DoSSUser>();
                        List<DoSSUser> users = doResumeService.getSsUserByUserID(uids);
                        for (DoSSUser user : users) {
                            if(user.getFid().equals(fid)){
                                user.setPointuser("");
                                user.setPointdate(null);
                                user.setPoint("");
                                user.setResult("");
                                user.setNotice(EnumTools.NoticeState.未发通知.ordinal());
                                user.setOp8("未公布");
                                user.setBz("");
                                editList.add(user);
                            }
                        }

                        if (editList.size() > 0) {
                            utilService.editList(editList);
                            utilService.printJsonResult(true, "操作成功！", "no", response);
                        } else {
                            utilService.printJsonResult(false, "当前选中信息不支持此操作！", "no", response);
                        }
                    } else {
                        utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败", "no", response);
        }
    }

    public DUserResume fillFowQAResult(DUserResume resume,HttpServletRequest request,ProjectInfoFlow nowflow,BaseUser operator){
        String flowName=nowflow.getFname();
        if (!StringTools.trimNotEmpty(flowName)){
            flowName=nowflow.getNickname();
        }
        if (flowName.equalsIgnoreCase(EnumTools.FlowNomalState.简历筛选.name())){
            resume.setState("未处理");
            // resume.setSxbz("");
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.笔试.name())){
            //
            // resume=fillExamResult(resume,newstate,newremark,nowflow,operator);
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.资料审核.name())){
            resume.setZlstate("未处理");
            resume.setZldate(null);
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.面试.name())){

        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.背景调查.name())||flowName.equalsIgnoreCase("政审")){
            resume.setZsstate("未处理");
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.实作.name())){
            // resume.
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.体检.name())){
            resume.setTjstate("未处理");
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.签约.name())){
            resume.setQystate("未处理");
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.实习.name())){
            resume.setSxstate("未处理");
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.录用.name())){
            resume.setBdstate("未处理");
        }
        return  resume;
    }
}
